#!/usr/bin/env bash
#
# Summary: Set or show the global Go version
#
# Usage: goenv global [<version>]
#
# Sets the global Go version. You can override the global version at
# any time by setting a directory-specific version with `goenv local'
# or by setting the `GOENV_VERSION' environment variable.
#
# <version> should be a string matching a Go version known to goenv.
# If no <version> is given, displays the global version if configured.
# <version> `system` unsets the previous version and displays it if configured.
# <version> `latest` sets the latest installed version (1.23.4).
# <version> `1` sets the latest installed major version (1.23.4).
# <version> `23` or `1.23` sets the latest installed minor version (1.23.4).
# <version> `1.23.4` sets this installed version (1.23.4).
# If no version can be found or no versions are installed or configured, an error message will be displayed.
# Run `goenv versions` for a list of available Go versions.

set -e
[ -n "$GOENV_DEBUG" ] && set -x

# Provide goenv completions
if [ "$1" = "--complete" ]; then
  echo latest
  echo system
  exec goenv-versions --bare
fi

versions=("$@")
GOENV_VERSION_FILE="${GOENV_ROOT}/version"

if [ -n "$versions" ]; then
  goenv-version-file-write "$GOENV_VERSION_FILE" "${versions[@]}"
else
  OLDIFS="$IFS"
  IFS=: versions=($(
    goenv-version-file-read "$GOENV_VERSION_FILE" ||
    goenv-version-file-read "${GOENV_ROOT}/global" ||
    goenv-version-file-read "${GOENV_ROOT}/default" ||
    echo system
  ))
  IFS="$OLDIFS"
  for version in "${versions[@]}"; do
    echo "$version"
  done
fi
